/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.forces.drag;

import ore.ForceModel;
import ore.SpacecraftState;
import ore.errors.OrekitException;
import ore.propagation.events.EventDetector;
import ore.propagation.numerical.TimeDerivativesEquations;

import org.apache.commons.math.geometry.Vector3D;

/** 
 * Atmospheric drag force model.
 * 
 * The drag acceleration is computed as follows :
 *
 * &gamma; = (1/2 * Ro * V<sup>2</sup> * S / Mass) * DragCoefVector
 *
 * With DragCoefVector = {Cx, Cy, Cz} and S given by the user through
 * the interface {@link DragSensitive}
 *
 * @author &Eacute;douard Delente
 * @author Fabien Maussion
 * @author V&eacute;ronique Pommier-Maurussane
 */
public class DragForce 
    extends Object
    implements ForceModel
{

    public final Atmosphere atmosphere;

    public final DragSensitive spacecraft;


    public DragForce(Atmosphere atmosphere,
                     DragSensitive spacecraft)
    {
        super();
        this.atmosphere = atmosphere;
        this.spacecraft = spacecraft;
    }


    /**
     * Compute the contribution of the drag to the perturbing acceleration.
     * 
     * @param s the current state information : date, kinematics, attitude
     * @param adder object where the contribution should be added
     * 
     * @exception OrekitException if some specific error occurs
     */
    public void addContribution(SpacecraftState s,
                                TimeDerivativesEquations adder)
        throws OrekitException
    {
        double rho =
            this.atmosphere.getDensity(s.getDate(), s.getPositionVelocity().getPosition(), s.getFrame());

        Vector3D vAtm =
            this.atmosphere.getVelocity(s.getDate(), s.getPositionVelocity().getPosition(), s.getFrame());

        Vector3D incidence = vAtm.subtract(s.getPositionVelocity().getVelocity());

        double v2 = Vector3D.dotProduct(incidence, incidence);

        Vector3D inSpacecraft =
            s.getAttitude().getRotation().applyTo(incidence.normalize());

        double k = rho * v2 * this.spacecraft.getDragCrossSection(s, inSpacecraft) / (2 * s.getMass());

        Vector3D cD = this.spacecraft.getDragCoef(s, inSpacecraft);

        /*
         * Addition of calculated acceleration to adder
         */
        adder.addXYZAcceleration(k * cD.getX(), k * cD.getY(), k * cD.getZ());
    }
    /** 
     * There are no discrete events for this model.
     * @return An empty array
     */
    public EventDetector[] getEventsDetectors() {
        return new EventDetector[0];
    }
}
